import { Body, Controller, Get, Post, Query, UploadedFile, UploadedFiles } from "@nestjs/common";
import { Express } from "express";
import { FileService } from "./file.service";
import { upload, fileMimetypeFilter, uploads } from "./upload.config";

@Controller("upload")
export class FileController {
  constructor(private readonly fileService: FileService) {}

  // 单文件上传
  @Post("file")
  @upload("file", {
    // limits: 对文件大小,文件名大小等限制
    limits: { fileSize: Math.pow(1024, 2) * 10 }, // 10MB 默认1MB
    // fileFilter() 对文件类型验证的函数
    fileFilter: fileMimetypeFilter(["image", "video", "document"]),
  })
  uploadFile(@UploadedFile() file: Express.Multer.File) {
    return this.fileService.uploadFile(file);
  }

  // 多文件上传
  @Post("files")
  @uploads("files", 5, {
    // limits: 对文件大小,文件名大小等限制
    limits: { fileSize: Math.pow(1024, 2) * 10 }, // 10MB 默认1MB
    // fileFilter() 对文件类型验证的函数
    fileFilter: fileMimetypeFilter(["image", "video", "document"]),
  })
  uploadFiles(@UploadedFiles() files: Array<Express.Multer.File>) {
    return this.fileService.uploadFiles(files);
  }

  // 大文件上传-多个
  @Post("bigFiles")
  @uploads("chunks", 5, {
    // limits: 对文件大小,文件名大小等限制
    limits: { fileSize: Math.pow(1024, 2) * 1024 * 6 }, // 6GB 默认1MB
    // fileFilter() 对文件类型验证的函数
    fileFilter: fileMimetypeFilter(["stream"]),
  })
  uploadBigFiles(@UploadedFiles() files: Array<Express.Multer.File>, @Body() body: { name: string }) {
    return this.fileService.uploadBigFiles(files, body.name);
  }
  // 合并大文件
  @Get("merge")
  async mergeFile(@Query("hash") hash: string) {
    return await this.fileService.mergeFile(hash);
  }
}
